package com.flower.wing.mainserver;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.swing.*;
import javax.swing.table.TableModel;

class MainDisplay extends JFrame implements ActionListener {

	/**
	 * 
	 */
	Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();
    
	private static Properties info;
	
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;

	private static final long serialVersionUID = 1L;
	JFrame jfr = null;
	JLabel forProcess = new JLabel("Data 확인 프로그램");
	JButton dbCon, shortcut01, shortcut02, shortcut03, shortcut04, shortcut05, shortcut06, checkServer, actionQuery;
	JTextField logName;
	JTextArea resultArea, queryField;
	ImageIcon exeIcon;
	Choice saveQuery;
	JScrollPane resultPane;
	JTable resultTable;
	
	private int x, y;
	private File file = null;

	public MainDisplay(){

		super("DB Management - developed by FlowerTeacher in WingFl.inc");

		launchFrame();

		setSize(1000, 600);
		x = screenSize.width / 2 - this.getWidth() / 2;  
        y = screenSize.height / 2 - this.getHeight() / 2;  
        setLocation(x, y);
		setVisible(true);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

	public void launchFrame(){

		dbCon = new JButton("Start Server");
		shortcut01 = new JButton("SQL_01");
		shortcut02 = new JButton("SQL_02");
		shortcut03 = new JButton("SQL_03");
		shortcut04 = new JButton("SQL_04");
		shortcut05 = new JButton("SQL_05");
		shortcut06 = new JButton("SQL_06");
		checkServer = new JButton("Server");
		
		JPanel panNorth = new JPanel();
		
		forProcess.setFont(new Font("바탕", Font.CENTER_BASELINE, 30));
		panNorth.add(forProcess);
		add(panNorth, BorderLayout.NORTH);

		JPanel panEast = new JPanel(new GridLayout(8,1));

		panEast.add(dbCon);
		dbCon.addActionListener(this);
		panEast.add(shortcut01);
		shortcut01.addActionListener(this);
		panEast.add(shortcut02);
		shortcut02.addActionListener(this);
		panEast.add(shortcut03);
		shortcut03.addActionListener(this);
		panEast.add(shortcut04);
		shortcut04.addActionListener(this);
		panEast.add(shortcut05);
		shortcut05.addActionListener(this);
		panEast.add(shortcut06);
		shortcut06.addActionListener(this);
		panEast.add(checkServer);
		checkServer.addActionListener(this);
		add(panEast, BorderLayout.EAST);

		JPanel panCenter = new JPanel();
		
		panCenter.setLayout(new BorderLayout());
		resultPane = new JScrollPane(resultTable = new JTable());
		panCenter.add(resultPane);
		add(panCenter, BorderLayout.CENTER);

		saveQuery = new Choice();
		saveQuery.add("default");
		saveQuery.add("shortcut01");
		saveQuery.add("shortcut02");
		saveQuery.add("shortcut03");
		saveQuery.add("shortcut04");
		saveQuery.add("shortcut05");
		saveQuery.add("shortcut06");

		JPanel panSouth = new JPanel();
		
		queryField = new JTextArea("", 5, 70);
		queryField.setEditable(true);
		exeIcon = new ImageIcon("img/apple.jpg");
		actionQuery = new JButton(exeIcon);
		panSouth.add(saveQuery);
		panSouth.add(queryField);
		panSouth.add(actionQuery);
		actionQuery.addActionListener(this);
		add(panSouth, BorderLayout.SOUTH);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == dbCon){
			try {
				dbConnect();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut01) {
			this.forProcess.setText(shortcut01.getText());
			String logSql = "SELECT * FROM user_info;";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut02) {
			this.forProcess.setText(shortcut02.getText());
			String logSql = "SELECT * FROM user_info";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut03) {
			this.forProcess.setText(shortcut03.getText());
			String logSql = "SELECT * FROM user_info";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut04) {
			this.forProcess.setText(shortcut04.getText());
			String logSql = "SELECT * FROM user_info";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut05) {
			this.forProcess.setText(shortcut05.getText());
			String logSql = "SELECT * FROM user_info";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut06) {
			this.forProcess.setText(shortcut06.getText());
			String logSql = "SELECT * FROM user_info";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == checkServer) {
			
			
			
		} else if (e.getSource() == actionQuery) {
			this.forProcess.setText("Action Query");
			String logSql = queryField.getText();
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	public void dbConnect() throws Exception{
		file = new File("./info/Info.properties");
		info = new Properties();
		info.load(new FileInputStream(file));
		Class.forName(info.getProperty("DBDRIVER"));
		conn = DriverManager.getConnection(info.getProperty("DBURL"),
				info.getProperty("DBID"), info.getProperty("DBPWD"));
		
		JOptionPane.showMessageDialog(jfr, "DB Connction Success");
	}
	
	public void getStatement() throws SQLException{
		this.stmt = conn.createStatement();
	}
	
	public void executeSQL(String exeSql) throws SQLException {
		this.getStatement();
		String sql = exeSql.trim();
		if(sql.lastIndexOf(";") > 0) 
			sql = sql.substring(0, sql.length() - 1);
		java.util.StringTokenizer st = 
			new java.util.StringTokenizer(sql);
		if(st.nextToken().toUpperCase().equals("SELECT")){
			ResultSet rs = stmt.executeQuery(sql);
			TableModel tm = new MyTableModel(rs);
			this.resultTable.setModel(tm);
		}else{
			int rowcount = stmt.executeUpdate(sql);
			JOptionPane.showMessageDialog(jfr, 
					rowcount + "메롱");
		}
		this.rs = this.stmt.executeQuery(sql);
	}

	public void dbClose() throws SQLException {
		this.conn.close();
	}
	
	public void stmtClose() throws SQLException {
		this.stmt.close();
	}
	
	public void rsClose() throws SQLException {
		this.rs.close();
	}
}